package nuc.zhangchao.test.controller;

import nuc.zhangchao.test.mapper.RoleMapper;
import nuc.zhangchao.test.pojo.Role;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Zhang Chao
 * @Date 2020/12/22 14:43
 * @Version 1.0
 */
@Controller
public class RoleController {

    @Resource
    private RoleMapper roleMapper;

    @GetMapping("/roleAll")
    public String findRole(Model model){
        List<Role> roleList = roleMapper.selectRoles();
        System.out.println(roleList);
        model.addAttribute("roleList",roleList);
        return "role_list";
    }

    @GetMapping("/toAddRole")
    public String toAddDept(){
        return "role_add";
    }  //跳转默认是转发
    //完成添加
    @PostMapping("/doAddRole")
    public String doAddRole(Role role){
        System.out.println(role);
        int i = roleMapper.addRole(role);
        System.out.println("执行结果："+i);
        return "redirect:/roleAll";
    }


    @GetMapping("/toUpdateRole")
    public String toUpdateRole(@RequestParam int id, Model model){
        Role role =roleMapper.selectRoleById(id);
        System.out.println(role);
        model.addAttribute("role",role);
        return "role_update";
    }
    @PostMapping("/doUpdateRole")
    public String doUpdateRole(Role role){
        int i=roleMapper.updateRole(role);
        System.out.println("执行结果:"+i);
        return "redirect:/roleAll";
    }

    //删除
    @GetMapping("/delRole")
    public String delRole(@RequestParam("id") String id){
        //从超链接过来的都是字符串
        int iid=Integer.parseInt(id);
        int i= roleMapper.delRole(iid);
        System.out.println("id:"+id);
        System.out.println("执行结果:"+i);
        return "redirect:/roleAll";
    }




}
